<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
class User extends Admin
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data = Db::table('lamp_user')
            ->select();

        $arr = [];
        foreach($data as $v){
            $roleid = Db::table('lamp_user_role')
                ->field('rid')
                ->where('uid', $v['id'])
                ->select();

            $roles = [];
            foreach($roleid as $val){
                $roles[] = Db::table('lamp_role')
                    ->field('name')
                    ->where([
                        'id'=>$val['rid'],
                        'status'=>'1'
                    ])
                    ->find();
            }
            $v['role'] = $roles;
            $arr[] = $v;
        }
//        var_dump($arr[0]['role']);die;

        return view('User/index', ['list'=>$arr]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add()
    {
        return view('User/add');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function doadd(Request $request)
    {
        $data = $request->post();
        if(empty($data['username'])){
            $this->error('用户名不能为空');
        }
        if(empty($data['name'])){
            $this->error('姓名不能为空');
        }
        if($data['userpass'] !== $data['reuserpass']){
            $this->error('两次密码不匹配');
        }
        $arr = [
            'username' => $data['username'],
            'name' => $data['name'],
            'userpass' => md5($data['userpass'])
        ];

        $list = Db::table('lamp_user')
            ->insert($arr);


        if($list){
            $this->success('添加成功','admin/User/index');
        }else{
            $this->error('添加失败');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function rolelist($id)
    {
        $data = Db::table('lamp_role')
            ->where('status', 1)
            ->select();

        $user = Db::table('lamp_user')
            ->where('id', $id)
            ->find();
//        var_dump($user);die;

        $rolelist = Db::table('lamp_user_role')
            ->field('rid')
            ->where('uid', $id)
            ->select();

        return view('User/rolelist', [
            'list' => $data,
            'role' => $rolelist,
            'user' => $user
        ]);
    }


    public function saverole(Request $request)
    {
        $p = $request->post();
        $role = $p['role'];
        $uid = $p['uid'];

        if(empty($role)){
            $this->error('请选择一个角色');
        }

        Db::table('lamp_user_role')
            ->where('uid', $uid)
            ->delete();

        foreach($role as $v){
            $data = [
                'uid' => $uid,
                'rid' => $v
            ];

            $list = Db::table('lamp_user_role')
                        ->insert($data);
        }
        if($list){
            $this->success('分配角色成功', 'admin/User/index');
        }else{
            $this->error('角色分配失败');
        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $data = Db::table('lamp_user')
                    ->where('id', $id)
                    ->delete();
        if($data){
            $result = Db::table('lamp_user_role')
                        ->where('uid', $id)
                        ->delete();
        }

        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . ' 的用户删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . ' 的用户删除失败,请重试!';
        }
        return json($info);
    }
}
